package com.pignest.hotel.util;

import java.util.List;

public class PageResult {
	private List data;
	private int beginIndex;// 起点
	private int pageSize;// 每页记录
	private Integer rowCount;// 总记录
	private int totalPage; // 总也数
	private int currentPage;// 当前页
	private int prePage; // 前页
	private boolean hasPreviousPage;// 有前页
	private int nextPage;// 下页
	private boolean hasNextPage;// 有下页
	// 设置
	private boolean isSelectJoinCount;// 有下页 
	//


	/**
	 * copy
	 */
	public PageResult(	PageResult p) {

		this.beginIndex = p.beginIndex;// 起点
		this.pageSize = p.pageSize;// 每页记录
		this.rowCount = p.rowCount;// 总记录
		this.totalPage = p.totalPage; // 总页数
		this.currentPage = p.currentPage;// 当前页
		this.prePage = p.prePage;// 前页
		this.hasPreviousPage = p.hasPreviousPage;// 有前页
		this.nextPage = p.nextPage;// 下页
		this.hasNextPage = p.hasNextPage;// 有下页
		// 设置
		this.isSelectJoinCount = p.isSelectJoinCount;// 有下页 

	}

	/** 
	 * 初始化 必须有 当前页 和 每页几条
	 */
	public PageResult() {
	
		this.currentPage = 1;
		this.pageSize = 10;
		initEL();
		
	}

	public PageResult(int currentPage) {
		this.currentPage = currentPage;
		this.pageSize = 10;
		initEL();
	}

	public PageResult(int currentPage, int pageSize) {
		this.currentPage = currentPage < 1 ? 1 : currentPage;
		this.pageSize = pageSize;

		initEL();
	}

	private void initEL() {
		rowCount = 0; // 总记录
		totalPage = 0;// 总页数
		beginIndex = 0;// 开始记录
		hasPreviousPage = false;// 有前页
		prePage = 0; // 前页
		hasNextPage = false;// 有下页
		nextPage = 0;// 下页
		isSelectJoinCount = false;// 查看1:n的N方关联本表的记录数 
	}

	/**
	 * 取得查询总数,起算其他值,
	 */
	public void setRowCount(Integer rowCount) {
		this.rowCount = rowCount;
		if (rowCount == 0) {
		 	initEL();
			return;
		}
		if (rowCount % pageSize == 0)
			totalPage = rowCount / pageSize;
		else
			totalPage = rowCount / pageSize + 1;
		// 初始化的可能会太大
		if (currentPage > totalPage)
			currentPage = totalPage;

		beginIndex = (currentPage - 1) * pageSize;

		if (currentPage == 1) {
			hasPreviousPage = false;
			prePage = 1;
		} else {
			hasPreviousPage = true;
			prePage = currentPage - 1;
		}

		if (currentPage == totalPage || totalPage == 0) {
			hasNextPage = false;
			nextPage = totalPage;
		} else {
			hasNextPage = true;
			nextPage = currentPage + 1;
		}

	}

	public int getBeginIndex() {
		return beginIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public int getPrePage() {
		return prePage;
	}

	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public Integer getRowCount() {
		return rowCount;
	}

	public List getData() {
		return data;
	}

	public void setData(List data) {
		this.data = data;
	}

	public void setPageSize(int pageSize) {
		if (pageSize > 20)
			pageSize = 20;
		this.pageSize = pageSize;
	}
	
	public void setPageSize(int pageSize,boolean returnALl) {
		if (pageSize > 9999)
			pageSize = 9999;
		this.pageSize = pageSize;
	}
	
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public boolean isSelectJoinCount() {
		return isSelectJoinCount;
	}

	public void setSelectJoinCount(boolean isSelectJoinCount) {
		this.isSelectJoinCount = isSelectJoinCount;
	}

	 


}
